package top.infopub.mgr.fia.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.infopub.generic.GenericDao;
import top.infopub.generic.GenericServiceImpl;
import top.infopub.mgr.common.page.ParamPage;
import top.infopub.mgr.common.vo.RspVo;
import top.infopub.mgr.fia.dao.BankInfoDao;
import top.infopub.mgr.fia.service.BankInfoService;
import top.infopub.mgr.fia.vo.BankInfoVo;
import top.infopub.model.fia.BankInfo;
import top.infopub.security.vo.AuthorizationVO;
import top.infopub.util.IdFactory;

import java.util.Date;
import java.util.List;

@Service
public class BankInfoServiceImpl extends GenericServiceImpl<BankInfo, String> implements
        BankInfoService {
	@Autowired
	private BankInfoDao bankInfoDao;
	
	@Override
	public GenericDao<BankInfo, String> getDao() {
		return bankInfoDao;
	}

    @Override
    public PageInfo<BankInfo> queryBankInfoPage(BankInfoVo bankInfoVo, ParamPage paramPage) {
        PageHelper.startPage(paramPage.getPage(), paramPage.getLimit());
        List<BankInfo> list = bankInfoDao.queryByWhereList(bankInfoVo);
        PageInfo<BankInfo> page = new PageInfo<>(list);
        return page;
    }

    @Override
    public RspVo modifyAccountStatus(String id, String accountStatus, AuthorizationVO vo) {
	    if (StringUtils.isBlank(id) || StringUtils.isBlank(accountStatus)) return RspVo.getFailureResponse(400, "缺少必填参数");
	    BankInfo bankInfo = bankInfoDao.selectByPrimaryKey(id);
	    if (bankInfo == null) return RspVo.getFailureResponse(400, "数据不存在~");
	    bankInfo.setUptBy(vo.getId());
	    bankInfo.setUptTime(new Date());
	    bankInfo.setAccountStatus(accountStatus);
	    bankInfoDao.modifyAccountStatus(bankInfo);
        return RspVo.getSuccessResponse();
    }

    @Override
    public List<BankInfo> queryBankInfoList(String accountStatus) {
        BankInfoVo bankInfoVo = new BankInfoVo();
        bankInfoVo.setAccountStatus(accountStatus);
        return bankInfoDao.queryByWhereList(bankInfoVo);
    }

    @Override
    public RspVo saveOrEdit(BankInfo bankInfo, AuthorizationVO vo) {
	    bankInfo.setCodes(IdFactory.getUUIDUpper());
	    bankInfo.setAddBy(vo.getId());
	    bankInfo.setAddTime(new Date());
	    bankInfo.setUptBy(vo.getId());
	    bankInfo.setUptTime(new Date());
	    if (StringUtils.isNotBlank(bankInfo.getId())) {
	        BankInfo tmp = bankInfoDao.selectByPrimaryKey(bankInfo.getId());
	        if (tmp == null) return RspVo.getFailureResponse(400, "数据不存在~");
	        bankInfoDao.updateByIdSelective(bankInfo);
        } else {
			BankInfo tmpBi = bankInfoDao.queryByAccountNo(bankInfo.getAccountNo());
			if (tmpBi != null) return RspVo.getFailureResponse(400, "开户行账号已存在~");
	        bankInfoDao.insert(bankInfo);
        }
        return RspVo.getSuccessResponse();
    }
}